package 算法.leetcode.bytedance;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lchenglong
 * @date 2022/5/6
 */
public class OfferString_I {

    public static void main(String[] args) {
        OfferString_I instance = new OfferString_I();
//        String s = "abcabcbb";
//        String s = "bbbb";
//        String s = "pwwkew";
        String s = "abba";
        int ans = instance.lengthOfLongestSubstring(s);
        System.out.println(ans);

    }
    public int lengthOfLongestSubstring(String s) {
        // 边界条件
        if (s == null||s.length()==0)return 0;
        if (s.length()==1)return 1;
        int start = 0,max = 0;
        Map<Character,Integer> visited = new HashMap<>(s.length());
        for (int i=0;i<s.length();i++) {
            Integer value = visited.get(s.charAt(i));
            if (value == null){
                // 之前不存在过
                visited.put(s.charAt(i),i);

            } else {
                // 之前存在过
                start = Math.max(value+1,start);
                visited.put(s.charAt(i),i);

            }
            max = Math.max(i-start+1,max);
        }
        return max;
    }
}
